import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

//解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
Vue.use(VueRouter)
}

    Vue.use(VueRouter)

    const routes = [
        {
            path: '/home',
            name: '/home',
            component: HomeView,
            children: [
                {
                    path: '/about',
                    name: 'about',
                    meta: {
                        name: '用户信息'
                    },
                    component: () => import('../views/AboutView.vue')
                },
                {
                    path: '/test01',
                    name: 'test01',
                    meta: {
                        name: '新闻信息'
                    },
                    component: () => import('../views/Test01.vue')
                },
                {
                    path: '/Booking',
                    name: 'Booking',
                    meta: {
                        name: '系统公告'
                    },
                    component: () => import('../views/Booking.vue')
                },
                {
                    path: '/News',
                    name: 'News',
                    meta: {
                        name: '水果销售'
                    },
                    component: () => import('../views/News.vue')
                },
                {
                    path: '/First',
                    name: 'First',
                    meta: {
                        name: '首页'
                    },
                    component: () => import('../views/First.vue')
                },
                {
                    path: '/drink',
                    name: 'drink',
                    meta: {
                        name: '系统日志'
                    },
                    component: () => import('../views/Drink.vue')
                },
                {
                    path: '/test',
                    name: 'test',
                    meta: {
                        name: '个人信息'
                    },
                    component: () => import('../views/Test.vue')
                },
                {
                    path: '/passwordview',
                    name: 'passwordview',
                    meta: {
                        name: '修改密码'
                    },
                    component: () => import('../views/PassWordView.vue')
                },
                {
                    path: '/403',
                    name: '403',
                    meta: {
                        name: '403'
                    },
                    component: () => import('../views/chanck/403.vue')
                }
            ]
        },
        {
            path: '/',
            name: 'Login',
            meta: {
                name: '登录'
            },
            component: () => import('../views/Login.vue')
        },
        {
            path: '/register',
            name: 'RegisterView',
            meta: {
                name: '注册'
            },
            component: () => import('../views/RegisterView.vue')
        },
        {
            path: '**',
            name: '404',
            meta: {
                name:'404'
            },
            component: () => import('../views/chanck/404.vue')
        },
    ]

    const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })

    router.beforeEach((to, from, next) => {
        console.log('to', to)
        //获取当前登录用户信息
        let adminPaths = ['/about', '/test01', '/Booking'];
        let user = JSON.parse(localStorage.getItem('user11') || '{}');
        if (user.role !== '管理员' && adminPaths.includes(to.path)) {
            next('/403')
            this.$message.error('没有访问权限，请重新登录')
        } else {
            next()
        }
    })

    export default router
